<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of admin
 *
 * @author 10520_000
 */
class admin extends CI_Controller {

    private $_url;
    private $_path;

    public function __construct() {
        parent::__construct();
        $this->load->helper('form');
        $this->_url = base_url('public/images/sanpham');
        $this->_path = realpath(APPPATH . '../public/images/sanpham');
    }

    public function sendmail() {
        if (1 == 1) {
            $subject = 'tieu de';
            $msg = 'anh di choi';

            $config = array(
                'protocol' => 'smtp',
                'smtp_host' => 'ssl://smtp.googlemail.com',
                'smtp_port' => 465,
                'smtp_user' => 'minhson.uit@gmail.com',
                'smtp_pass' => '@ms270892'
            );
            $config = array(
                'protocol' => 'smtp',
                'smtp_host' => 'ssl://smtp.googlemail.com',
                'smtp_port' => 465,
                'smtp_user' => 'luckylee307@@gmail.com',
                'smtp_pass' => 'chutlo93',
                'mailtype' => 'html',
                'charset' => 'iso-8859-1'//Nhớ đánh đúng user và pass nhé
            );
            $this->load->library('email', $config);
            $this->email->set_newline("\r\n");
            $this->email->from('minhson.uit@gmail.com', 'Si ha shopping');
            $this->email->to('minhson.uit@gmail.com');
            $this->email->subject($subject);
            $this->email->message($msg);
            if ($this->email->send()) {
                echo "mail da duoc gui";
            } else {
                show_error($this->email->print_debugger());
            }
        } else {
            $data['title'] = 'Send Email ';
            $data['main'] = 'admin_subs_mail';
            $this->load->view('dashboard', $data);
        }
    }

    public function addsanpham() {

        /* $this->load->model('mSanpham');
          $sp = new MSanpham();
          $sp->set_giaban($data);
          $sp->set_hinhanh($data);
          $sp->set_is_noibat($data);
          $sp->set_machitiet($data);
          $sp->set_mahangsx($data);
          $sp->set_makm($data);
          $sp->set_matl($data);
          $sp->set_tensp($data); */
        $this->load->view('admin/addsanpham');
        /*
         * config upload image
         */
    }

    /*
     * upload image
     */

    public function view() {
        $files = scandir($this->_path);
        $files = array_diff($files, array('.', '..', 'thumbs'));

        // $data['files']=$files;
        // $this->load->view('viewupload',$data);
        //echo json_encode($files);
        // var_dump($files);
        // $data=array('files'=>$files);
        // var_dump($files);
        $data = array(1 => "abc", 2 => 'cdf', 3 => 'Điện thoại', 4 => '\u0110i\u1ec7n tho\u1ea1i');
        $files = array_map('utf8_encode', $files);
        //   echo "<br> xx";
        //  echo json_encode($data, JSON_ERROR_UTF8);
        //  echo "<br> xx";
        echo json_encode($files);
        //   echo json_last_error_msg();
        // json_decode($files);
    }

    public function sanpham() {

        if (isset($_POST['submit'])) {
            $this->load->model('mSanpham');
            $submit = $_POST['submit'];
            $sp = new MSanpham();
            switch ($submit) {
                case 'add':// hàm add sản phẩm
                    if (isset($_POST['masp']) && isset($_POST['tensp']) && isset($_POST['giaban']) && isset($_POST['mahangsx']) && isset($_POST['matl'])) {


                        $masp = $_POST['masp'];
                        $tensp = $_POST['tensp'];
                        $giaban = $_POST['giaban'];
                        $mahangsx = $_POST['mahangsx'];
                        $matl = $_POST['matl'];
                        $hinhanh = $_POST['hinhanh'];
                        $sp->OnCreate();
                        $sp->set_giaban($giaban);
                        $sp->set_hinhanh($hinhanh); /*
                          $sp->set_is_noibat($data);
                          $sp->set_machitiet($data); */
                        $sp->set_mahangsx($mahangsx);
                        $sp->set_masp($masp);
                        $sp->set_matl($matl);
                        $sp->set_tensp($tensp);
                        $submit = $_POST['submit'];
                        if ($submit == 1)
                            var_dump($sp->save());
                        elseif ($submit == 2)
                            echo 'update';
                    }
                    break;
                case 'update':
                    break;
                case 'delete':
                    if (isset($_POST['masp'])) {
                        $masp = $_POST['masp'];
                        // var_dump($sp->DeleteByID($masp));
                        echo json_encode(array('result' => $sp->DeleteByID($masp)));
                    }
                    break;
                case 'search':
                    if (isset($_POST['keyword'])) {

                        $keyword = $_POST['keyword'];
                        $result = $sp->SearchByIDorName($keyword);
                        // var_dump($result);
                        // header("Content-type:application/json"); 

                        echo json_encode($result);
                        // echo json_last_error();
                    }
                    break;
                case 'getsp':
                    if (isset($_POST['masp'])) {
                        $masp = $_POST['masp'];
                        $result = $sp->getByID($masp);
                        //var_dump($result);
                        echo json_encode($result);
                    }
                    break;
                default:
                    break;
            }
        } else {
            $files = scandir($this->_path);
            $files = array_diff($files, array('.', '..', 'thumbs'));
            $this->load->model('msanpham');
            $this->load->model('mloaisanpham');
            $this->load->model('mhangsanxuat');
            $loaisp = new MLoaisanpham();
            $hangsx = new MHangsanxuat();
            $sp = new MSanpham();
            $data = array();
            $data['sanphams'] = $sp->GetAllSanpham();
            $data['loaisps'] = $loaisp->getAllLoaisp();
            $data['hangsxs'] = $hangsx->getAllhangsx();
            $data['lstview']=array('sanpham'=> $this->load->view("admin/sanpham", $data, TRUE));
           // $data['sanpham'] = $this->load->view("admin/sanpham", $data, TRUE);
            $data['loaisps'] = $loaisp->getAllLoaisp();
            $data['files'] = $files;
            $data['viewupload'] = $this->load->view('admin/viewupload', $data, TRUE);
            $this->load->view('admin/layout', $data);
        }
    }
    public function hangsx(){
         if (isset($_POST['submit'])) {             
            $this->load->model('mHangsanxuat');
            $submit = $_POST['submit'];
            $hangsx = new MHangsanxuat();
            
            switch ($submit) {
                case 'add':// hàm add sản phẩm
                    if (isset($_POST['mahangsx']) && isset($_POST['tenhangsx']) ) {
                        $mahangsx = $_POST['mahangsx'];
                        $tenhangsx = $_POST['tenhangsx'];
                        $hangsx->OnCreate();
                        $hangsx->set_mahangsx($mahangsx);
                        $hangsx->set_tenhangsx($tenhangsx);
                        echo json_encode(array('result'=> $hangsx->Insert($mahangsx, $tenhangsx)));
                    }
                    break;
                case 'update':
                    break;
                case 'delete':
                  
                   if (isset($_POST['mahangsx'])) {
                        $mahangsx = $_POST['mahangsx'];
                        echo json_encode(array('result' => $hangsx->DeleteByID($mahangsx)));
                    }
                    break;
                case 'search':
                    if (isset($_POST['keyword'])) {

                        $keyword = $_POST['keyword'];
                        $result = $hangsx->SearchByIDorName($keyword);
                        // var_dump($result);
                        // header("Content-type:application/json"); 

                        echo json_encode($result);
                        // echo json_last_error();
                    }
                    break;
                case 'get':
                    if (isset($_POST['mahangsx'])) {
                        $mahangsx = $_POST['mahangsx'];
                        $result = $hangsx->getByID($mahangsx);
                        //var_dump($result);
                        echo json_encode($result);
                    }
                    break;
                default:
                    break;
            }
        } else
        {
            $data=array();
            $data['lstview']=array('sanpham'=> $this->load->view("admin/hangsanxuat", $data, TRUE));                      
            $this->load->view('admin/layout', $data);
        }
    }
     public function loaisp(){
         if (isset($_POST['submit'])) {             
            $this->load->model('MLoaisanpham');
            $submit = $_POST['submit'];
           
            $loaisp = new MLoaisanpham();
            
            switch ($submit) {
                case 'add':// hàm add loại sản phẩm
                    if (isset($_POST['matl']) && isset($_POST['tentl']) ) {
                        $matl = $_POST['matl'];
                        $tentl = $_POST['tentl'];
                        $loaisp->OnCreate();
                        $loaisp->set_matl($matl);
                        $loaisp->set_tentl($tentl);
                        echo json_encode(array('result'=> $loaisp->Insert($matl, $tentl)));
                    }
                    break;
                case 'update':
                    if (isset($_POST['matl']) && isset($_POST['tentl']) ) {
                        $matl = $_POST['matl'];
                        $tentl = $_POST['tentl'];
                        $loaisp->OnCreate();
                        $loaisp->set_matl($matl);
                        $loaisp->set_tentl($tentl);
                        echo json_encode(array('result'=> $loaisp->UpdateByID ()));
                    }
                    break;
                case 'delete':
                  
                   if (isset($_POST['matl'])) {
                        $matl = $_POST['matl'];                    
                        echo json_encode(array('result' => $loaisp->DeleteByID($matl)));
                    }
                    break;
                case 'search':
                    if (isset($_POST['keyword'])) {

                        $keyword = $_POST['keyword'];
                        $result = $loaisp->SearchByIDorName($keyword);                       
                        echo json_encode($result);                        
                    }
                    break;
                case 'get':
                    if (isset($_POST['matl'])) {
                        $matl = $_POST['matl'];
                        $result = $loaisp->getByID($matl);
                        //var_dump($result);
                        echo json_encode($result);
                    }
                    break;
                default:
                    break;
            }
        } else
        {
            $data=array();
            $data['lstview']=array('loaisanpham'=> $this->load->view("admin/loaisanpham", $data, TRUE));                      
            $this->load->view('admin/layout', $data);
        }
    }

    public function viewupload() {
        $files = scandir($this->_path);
        $files = array_diff($files, array('.', '..', 'thumbs'));
        $this->load->model('msanpham');
        $this->load->model('mloaisanpham');
        $loaisp = new MLoaisanpham();
        $sp = new MSanpham();
        $data = array();
        $data['sanphams'] = $sp->GetAllSanpham();

        $data['loaisps'] = $loaisp->getAllLoaisp();
        $data['files'] = $files;
        $data['viewupload'] = $this->load->view('admin/viewupload', $data, TRUE);
        $data['themsanpham'] = $this->_viewthemsanpham();
        //   $data['loaisanpham']=  $this->load->view('admin/loaisanpham',$data,TRUE);
        $this->load->view('admin/layout', $data);
//        $data['sp_view']=$this->load->view('admin/sp_view',$data,TRUE);
//        //$data['views']=array('admin/sp_view');
//        $this->load->view('admin/content',$data);
        // $this->load->view('admin/viewupload');
        //var_dump($files);
//        foreach ($files as $pic) {
//            $p=  base_url("public/images/sanpham/")."/".$pic; echo "<img src='$p'/>";
//            
//        }
    }

    public function upload() {
        $config['upload_path'] = $this->_path;
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '1000';
        $config['max_width'] = '1024';
        $config['max_height'] = '768';
        $this->load->library('upload', $config);
        // var_dump($_FILES);
//        if(!$this->upload->do_upload('img')){
//            $error=  $this->upload->display_errors();
//            var_dump($error);
//        }
//        else
//        {
//           // echo 'success';
//        }
        $files = scandir($this->_path);
        $files = array_diff($files, array('.', '..', 'thumbs'));
        $this->load->model('msanpham');
        $this->load->model('mloaisanpham');
        $loaisp = new MLoaisanpham();
        $sp = new MSanpham();
        $data = array();
        $data['sanphams'] = $sp->GetAllSanpham();
        ;
        $data['loaisps'] = $loaisp->getAllLoaisp();
        $data['files'] = $files;
        $data['themsanpham'] = $this->_viewthemsanpham();
        $data['sp_view'] = $this->load->view('admin/sp_view', $data, TRUE);
        $data['loaisanpham'] = $this->load->view('admin/loaisanpham', $data, TRUE);
        $data['views'] = array('admin/sp_view');
        $this->load->view('admin/content', $data);
    }

    private function _viewthemsanpham() {
        $this->load->model('mloaisanpham');
        $this->load->model('mhangsanxuat');
        $hangsx = new MHangsanxuat();
        $loaisp = new MLoaisanpham();
        $data = array();
        $data['loaisps'] = $loaisp->getAllLoaisp();
        $data['hangsxs'] = $hangsx->getAllhangsx();
        return $this->load->view("admin/sanpham", $data, TRUE);
    }

}

?>
